package com.hl.web;

import com.hl.pojo.Psychologicalassessments;
import com.hl.service.XinlicepingService;
import com.hl.util.GeneralResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Xinli")
@CrossOrigin
public class XinlicepingController {
    String path = null;
    File toFile = null;
    @Autowired
    public XinlicepingService xinlicepingService;

    /*小程序查询*/
    @RequestMapping("/ceping")
    public GeneralResult xinlicepingList(){

        List<Psychologicalassessments>  list = xinlicepingService.XinlicepingListAll();

        System.out.println(list);

        return GeneralResult.success(list);
    }
    /*后台数据展示*/
    @RequestMapping("/xinliceping")
    public GeneralResult xinlicepingListAll(){
        return GeneralResult.success(xinlicepingService.xinicepingandall());
    }
    /*后台上传图片存放路径*/
    @RequestMapping("/upload")
    public GeneralResult upload(MultipartFile file) throws IOException {
        toFile = new File("C:\\Users\\Administrator\\Desktop\\sfzy-vue\\vue\\sfzy\\public\\images\\"+file.getOriginalFilename());
        file.transferTo(toFile);
        path="/images/"+file.getOriginalFilename();
        System.out.println(file.getOriginalFilename());
        System.out.println(toFile.getAbsolutePath());
        return GeneralResult.success();
    }
    /*后台新增*/
    @RequestMapping("/insertall")
    public GeneralResult insertall(@RequestBody Map<String,String> map){
        String name = map.get("name");
        String question = map.get("question");
        Integer id = Integer.parseInt(map.get("id"));
        String answer = map.get("answer");
        List<Psychologicalassessments>  list = xinlicepingService.XinlicepingListAll();
        for(Psychologicalassessments psychologicalassessments : list){
            if (id.equals(psychologicalassessments.getId())){
                xinlicepingService.insertwenda(question,id,answer);
                return GeneralResult.success();
            }
        }
        xinlicepingService.insertxinli(id,name,path);
        xinlicepingService.insertwenda(question,id,answer);
        return GeneralResult.success("添加成功");
    }

    @RequestMapping("/deleteceping")
    public GeneralResult deleteceping(@RequestBody Map<String,String> map){
        Integer id = Integer.parseInt(map.get("cepingid"));
        return GeneralResult.success("删除成功",xinlicepingService.deleteceping(id));
    }

    @RequestMapping("/deletecwenda")
    public GeneralResult deletecwenda(@RequestBody Map<String,String> map){
        Integer id = Integer.parseInt(map.get("shanchuid"));
        return GeneralResult.success("删除成功",xinlicepingService.deletewenda(id));
    }

    @RequestMapping("/updateceping")
    public GeneralResult updateceping(@RequestBody Map<String,String> map){
        Integer id = Integer.parseInt(map.get("cepingid"));
        String name = map.get("name");
        return GeneralResult.success("修改成功",xinlicepingService.updateceping(id, name));
    }


}
